﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Forms.WinAPI;

namespace UAS5.UControls
{
    public partial class frmKeyMouse : Form
    {
        public frmKeyMouse()
        {
            InitializeComponent();
        }

        private void frmKeyMouse_Load(object sender, EventArgs e)
        {
            HotKeyRegistion();
            // 注册热键为Alt+R 运行
            HotkeyHelper.RegisterHotKey(Handle, 124, HotkeyHelper.KeyModifiers.Alt, Keys.R);
            // 注册热键为Alt+C 停止
            HotkeyHelper.RegisterHotKey(Handle, 125, HotkeyHelper.KeyModifiers.Alt, Keys.C);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        #region 热键管理

        /// <summary>
        /// 热键管理 2013-7-9 by sutroon
        /// </summary>
        private void HotKeyRegistion()
        {
            // 注册热键为F9  左键单击
            HotkeyHelper.RegisterHotKey(Handle, 120, HotkeyHelper.KeyModifiers.None, Keys.F9);
            // 注册热键为F10 右键单击
            HotkeyHelper.RegisterHotKey(Handle, 121, HotkeyHelper.KeyModifiers.None, Keys.F10);
            // 注册热键为F11 左键双击
            HotkeyHelper.RegisterHotKey(Handle, 122, HotkeyHelper.KeyModifiers.None, Keys.F11);
            // 注册热键为F12 防止休眠
            HotkeyHelper.RegisterHotKey(Handle, 123, HotkeyHelper.KeyModifiers.None, Keys.F12);
        }

        private void HotkeyUnRegistion()
        {
            HotkeyHelper.UnregisterHotKey(Handle, 120);
            HotkeyHelper.UnregisterHotKey(Handle, 121);
            HotkeyHelper.UnregisterHotKey(Handle, 122);
            HotkeyHelper.UnregisterHotKey(Handle, 123);
        }

        /// <summary>
        /// 重写WndProc()方法，通过监视系统消息，来调用过程
        /// 监视Windows消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            //如果m.Msg的值为0x0312那么表示用户按下了热键
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToString())
                    {
                        case "120": // 左键单击
                            MouseEventHelper.mouse_event((int)(MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTDOWN | MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTUP), 0, 0, 0, 0);
                            break;
                        case "121": // 右键单击
                            MouseEventHelper.mouse_event((int)(MouseEventHelper.KeyModifiers.MOUSEEVENTF_RIGHTDOWN | MouseEventHelper.KeyModifiers.MOUSEEVENTF_RIGHTUP), 0, 0, 0, 0);
                            break;
                        case "122": // 左键双击
                            MouseEventHelper.mouse_event((int)(MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTDOWN | MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTUP), 0, 0, 0, 0);
                            MouseEventHelper.mouse_event((int)(MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTDOWN | MouseEventHelper.KeyModifiers.MOUSEEVENTF_LEFTUP), 0, 0, 0, 0);
                            break;
                        case "123": // 鼠标移动
                            MouseEventHelper.mouse_event((int)(MouseEventHelper.KeyModifiers.MOUSEEVENTF_ABSOLUTE | MouseEventHelper.KeyModifiers.MOUSEEVENTF_MOVE), 500, 500, 0, 0);
                            break;
                        case "124": // 注册热键
                            HotKeyRegistion();
                            break;
                        case "125": // 卸载热键
                            HotkeyUnRegistion();
                            break;
                    }
                    break;
            }

            // 将系统消息传递自父类的WndProc
            base.WndProc(ref m);
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmKeyMouse_FormClosing(object sender, FormClosingEventArgs e)
        {
            HotkeyHelper.UnregisterHotKey(Handle, 124);
            HotkeyHelper.UnregisterHotKey(Handle, 125);
        }
    }
}
